package com.tedu.api.d5_io_seri;

import java.io.*;

/**本类测试序列化和反序列化
 * 本类测试序列化和反序列化
 */
public class Test1_SERI {
    public static void main(String[] args) {
        method1();//测试序列化
        method2();//测试反序列化
    }

    /**创建测试序列化的方法*/
    public static void method1() {
        ObjectOutputStream out=null;
        //创建ObjectOutputStream流对象来完成序列化
        try {
            out=new ObjectOutputStream(
                    new FileOutputStream("d:/ready/1.txt"));

            //创建对象
            Student s=new Student("孙悟空",200,"男","花果山");
            //通过OOS完成序列化
            out.writeObject(s);
            System.out.println("已写入序列化对象");

        } catch (IOException e) {
            System.out.println("失败！！！");
            e.printStackTrace();
        }finally {
            //释放资源
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
    /**创建测试反序列化的方法*/
    public static void method2() {
        ObjectInputStream in=null;

        try {
            in=new ObjectInputStream(new FileInputStream("d:/ready/1.txt"));

            Object o = in.readObject();

            System.out.println(o);//@27d6c5e0
            System.out.println("反序列化成功");

        } catch (Exception e) {
            System.out.println("反序列化失败！");
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
